package com.example.demo.utils.exceptionHandler;

import com.example.demo.utils.exception.TException;
import com.example.demo.utils.exception.ServiceException;
import com.example.demo.utils.exception.ValidatedException;
import com.example.demo.utils.msg.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class DefaultExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(ValidatedException.class)
    public Message processValidatedException(ValidatedException e) {
        LOGGER.error("参数异常:{}",e.getMessage());
        return new Message(1601,e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    public Message processServiceException(ServiceException e) {
        LOGGER.error("业务异常:{}",e.getMessage());
        return new Message(1603,e.getMessage());
    }

    @ExceptionHandler(TException.class)
    public Message handlerSellerException(TException e) {
        LOGGER.error("XX模块异常:{}",e.getMessage());
        return new Message(e.getCode(),e.getMessage());
    }







    @ExceptionHandler(RuntimeException.class)
    public Message processRuntimeException(RuntimeException e) {
        e.printStackTrace();
        LOGGER.error("运行时异常:{}",e.getMessage());
        return new Message(1604,"操作失败");
    }

    @ExceptionHandler(Exception.class)
    public Message processException(Exception e) {
        e.printStackTrace();
        LOGGER.error("系统异常:{}",e.getMessage());
        return new Message(1604,"操作失败");
    }
}
